//
//  ConsultTableView.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-29.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "ConsultTableView.h"
#import "ConsultTableCell.h"

@interface ConsultTableView()
{
    BOOL isSend;
}
@end

@implementation ConsultTableView

@synthesize superVC;

-(void)setAutoDataArray:(NSArray *)dArray
{
    CGFloat bottom1 = self.mainTable.contentSize.height-self.mainTable.contentOffset.y-self.mainTable.frame.size.height;
    NSInteger total = self.autoDataArray.count;

    _autoDataArray = dArray;
    [self.mainTable reloadData];
    [self performSelector:@selector(recoverShowState) withObject:nil afterDelay:0];
    
    BOOL showBottom = NO;
    if (isSend) {
        showBottom = YES;
    }else{
        if (total == 0 && dArray.count>0) {
            //此状态是第一次加载数据
            //下面数据最新
            showBottom = YES;
        }
        else if (total != self.autoDataArray.count){
            //判断此时刷新完数据之后
            //当前显示位置不变
            CGFloat bottom2 = self.mainTable.contentSize.height-self.mainTable.contentOffset.y-self.mainTable.frame.size.height;
            if (bottom1 < bottom2) {
                CGFloat y = self.mainTable.contentSize.height - self.mainTable.frame.size.height - bottom1;
                if (y>=0 && y<(self.mainTable.contentSize.height-self.mainTable.frame.size.height)) {
                    [self.mainTable setContentOffset:CGPointMake(0, y)];
                }
            }
        }
    }
    
    if (showBottom) {
        //显示底部数据
        if (self.mainTable.contentSize.height > self.mainTable.frame.size.height) {
            [self.mainTable setContentOffset:CGPointMake(0, self.mainTable.contentSize.height-self.mainTable.frame.size.height)];
        }
    }
    
    isSend = NO;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:superVC.saveDataText object:nil userInfo:@{@"data": _autoDataArray}];
}

//添加刚发送的消息
-(void)addSendMsg:(ConsultInfo*)info
{
    isSend = YES;
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.autoDataArray];
    [array addObject:info];
    info.pubtime = [z_DateTime encodeTime:[NSDate date]];
    self.autoDataArray = array;
}

//没有消息,刷新view
-(void)noMsgRefreshView
{
    //没有最新的未读消息
    ConsultInfo *info = [[ConsultInfo alloc] init];
    if (superVC.consultType == FEEDBACK_TYPE) {
        info.content = @"若希望得到更快的回复，请留下您的联系方式或加入QQ群：62358412";
    }else{
        info.content = @"您好，请问有什么可以帮助您？";
//        info.time = [z_DateTime encodeTime:[NSDate date]];
    }
    info.isreply = @"-1";
    self.autoDataArray = @[info];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.autoDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.section];
    ConsultTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        CGSize size = [tableView rectForRowAtIndexPath:indexPath].size;
        cell = [[ConsultTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier size:size];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    ConsultInfo *info = [self.autoDataArray objectAtIndex:indexPath.row];
    [cell setContent:info myImage:APP_USER_MSG.headimg];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConsultInfo *info = [self.autoDataArray objectAtIndex:indexPath.row];
    if (info.height == 0) {
        [self calculateCellHeight:info];
    }
    return info.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 计算CELL的高度
//计算cell的高度
-(void)calculateCellHeight:(ConsultInfo*)info
{
    NSString *title = info.content;
    if (!title) {
        title = info.title;
    }
    
    //只显示信息
    CGFloat height = 36;
    if (info.pubtime) {
        height = 65;
    }
    
    CGSize mySize = [z_UI returnHeightFloat:title frontSize:[UIFont boldSystemFontOfSize:14] frontWidth:160];
    height+=mySize.height;
    info.height = height;
}


@end
